<?php
namespace zake\server\http;

use zake\helper\Console;
use zake\foundation\reactor\trigger\BeeConnect;


class HttpConnect extends BeeConnect implements HttpHandler
{

    /**
     * head 最大长度
     *
     * @var int
     */
    public $headMaxLength = 1000;
    public $bodyMaxLength = 10000;


    public function __destruct()
    {
        //$sessionId = $this->getSessionId();
        //$connectId = $this->getConnectId();
        //$output = "connect id:[{$connectId}]. session id:[{$sessionId}] ";
        //Console::success("http connect stream __destruct. {$output}");
    }



    public function onCloseHandler()
    {
        $fd = $this->getFd();
        Console::warnning("客户端主动关闭: fd[{$fd}]");
    }


    /**
     * 消息包回调
     * @param $buffer
     *
     * @author liu.bin 2020-05-06 16:30
     */
    public function onBufferHandler($buffer)
    {
        $requestPath = $this->_parsePathInfo($buffer);
        $requestInfo = parse_url($requestPath);
        $callback = $this->getListener()->getHandFunc($requestInfo['path']);
        if($callback instanceof \Closure){
            $callback($requestInfo['path'],$this);
            return;
        }
    }



    private function _parsePathInfo($buffer)
    {
        if(strpos($buffer,"\r\n")){

            list($line,) = explode("\r\n",$buffer,2);
            list($method,$path,$httpv) = explode(" ",$line);
            return $path;
        }
        return "no header";
    }


}
